package com.g8.pethospitallearnbackend.interceptors;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.g8.pethospitallearnbackend.exception.CustomException;
import com.g8.pethospitallearnbackend.utils.JWTUtil;
import com.g8.pethospitallearnbackend.utils.RedisUtil;
import com.g8.pethospitallearnbackend.utils.responseUtil.ResponseStatusEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component //注册拦截器 将其放入ioc容器中
public class JWTinterceptor implements HandlerInterceptor {
//    private final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//    private final RedisUtil redisUtil = new RedisUtil(redisTemplate);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Map<String, Object> map = new HashMap<>();
        // 获取请求头中的令牌
        String token = request.getHeader("token");
        log.info("请求头中的token是:{}",token);

        try {
            if(token==null){
                throw new CustomException(ResponseStatusEnum.MISS_PARAM, "token为空");
            }

//            // 先看看redis黑名单里有无（即注销了的）
//            redisTemplate.afterPropertiesSet();
//            Object expTokenInRe = redisUtil.get("expiredToken");
//            if (expTokenInRe instanceof ArrayList<?>) {
//                for (Object o : (List<?>) expTokenInRe) {
//                    if (token.equals(o)) throw new CustomException(ResponseStatusEnum.MISS_PARAM, "该token已登出");
//                }
//            }

            DecodedJWT verify = JWTUtil.verify(token);
            return true;
        } catch (SignatureVerificationException e){
            //e.printStackTrace();
            throw new CustomException(ResponseStatusEnum.SYSTEM_ERROR, "无效签名");
        } catch (TokenExpiredException e){
            //e.printStackTrace();
            throw new CustomException(ResponseStatusEnum.SYSTEM_ERROR, "token已过期");
        } catch (AlgorithmMismatchException e){
            //e.printStackTrace();
            throw new CustomException(ResponseStatusEnum.SYSTEM_ERROR, "token算法不一致");
        } catch (Exception e){
            //e.printStackTrace();
            throw new CustomException(ResponseStatusEnum.SYSTEM_ERROR, "token无效："+e.getMessage());
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
